package com.tdam2011.grupo06.control;

import java.io.StringWriter;
import java.util.Date;
import java.util.List;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xmlpull.v1.XmlSerializer;

import android.util.Xml;

import com.tdam2011.grupo06.model.MensajeBob;
import com.tdam2011.grupo06.network.Networking;

public class BobMessageParserSax {
	
	public BobMessageParserSax()
	{
		
	}
	
	public List<MensajeBob> parseMessages(String user, String password, Date timeFilter)
	{
		SAXParserFactory factory = SAXParserFactory.newInstance();

		try
		{
			SAXParser parser = factory.newSAXParser();
			BobMessageHandler handler = new BobMessageHandler();
			parser.parse(Networking.requestGetMessages(user, password, timeFilter), handler);
			return handler.getMensajes();
		}
		catch (Exception e)
		{
			throw new RuntimeException(e);
		}
	}
	
	
	@SuppressWarnings("unused")
	private String writeXml(String user, String key){
	    XmlSerializer serializer = Xml.newSerializer();
	    StringWriter writer = new StringWriter();
	    try {
	        serializer.setOutput(writer);
	        serializer.startDocument("UTF-8", true);
	        serializer.startTag("", "action");
	        serializer.attribute("", "id", "REQUEST_RANDOM_VALUE");
	        serializer.attribute("", "name", "get-messages");
	        serializer.startTag("", "action-detail");
	        serializer.startTag("", "auth");
	        serializer.attribute("", "username", "marcelo");
	        serializer.attribute("", "key", "123456");
	        serializer.endTag("", "auth");
	        serializer.startTag("", "filter");
	        serializer.attribute("", "type", "timestamp");
	        serializer.text("17/11/2011 20:00:00");
	        serializer.endTag("", "filter");
	        serializer.endTag("", "action-detail");
	        serializer.endTag("", "action");
	        serializer.endDocument();
 
	        String result = writer.toString();
	        return result;
	    } catch (Exception e) {
	        throw new RuntimeException(e);
	    } 
	}
}
